package org.itboys.mobile.controller.mobile;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.util.MobileSignUtils;
import org.itboys.mobile.dto.SundayMemberDto;
import org.itboys.mobile.dto.order.KitchenOrderDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.mobile.SundayMobileMemberService;
import org.itboys.mobile.service.pos.PosSaleService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_用户_controller。
 */
@Controller
@RequestMapping("/sunday/mobile/member")
public class SundayMobileMemberController extends BaseController{
    @Autowired
    private SundayMobileMemberService memberService;
    @Autowired
    private PosSaleService posSaleService;

    /**
     * 【用户版】用户注册。手机号和密码
     * @param request
     * @param response
     * @param mobile
     * @param password
     * @param code
     * @param villageId
     */
    @RequestMapping(value="/register", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】用户注册。手机号和密码。-1直接提示消息")
    public void register(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                     @ApiParam(required=true, name="password", value="密码。明文")@RequestParam(value = "password", required = true)String password,
                     @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
                     @ApiParam(required=true, name="villageId", value="小区ID")@RequestParam(value = "villageId", required = true)Long villageId
                         ){
        try{
            /*Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            SundayMemberDto member= memberService.register( mobile,password,code,villageId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(member,CommonConstants.SUCCESS));
        }catch (Exception e){

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/mobile/member/register--------"+e);
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }
    /**
     * 【用户版和管理版】用户登陆。手机号和密码。-1直接提示消息
     * @param request
     * @param response
     * @param mobile
     * @param password
     */
    @RequestMapping(value="/login", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】用户登陆。手机号和密码。-1直接提示消息")
    public void login(HttpServletRequest request,
                         HttpServletResponse response,
                         @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                         @ApiParam(required=true, name="password", value="密码。明文")@RequestParam(value = "password", required = true)String password,
                         @ApiParam(required=true, name="loginType", value="登陆类型。1用户版，2管理版")@RequestParam(value = "loginType", required = true)Integer loginType

    ){
        try{
           /* Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            //Long memberId = AccountIdHoldUtils.getMemberId();
            SundayMemberDto member= memberService.login( mobile,password,loginType);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/login----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第第二步。
     * @param request
     * @param response
     * @param thirdType
     * @param unionId
     */
    @RequestMapping(value="/loginWithThirdOne", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第二步。")
    public void loginWithThirdOne(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                      @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId){

        try{

            SundayMemberDto member= memberService.loginWithThirdOne(thirdType, unionId);
            if(member==null){
                ResponseMessage.error(-2,"需要验证手机号");
            }else{
                ResponseMessage.success(member);
            }

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/loginWithThirdOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     */
    @RequestMapping(value="/loginWithThirdTwo", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。")
    public void loginWithThirdTwo(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code){

        try{

            SundayMemberDto member= memberService.loginWithThirdTwo( thirdType, nickName, image,  unionId,  mobile, code);
            if(member==null){
                ResponseMessage.error(-2,"需要选择您所在的小区");
            }else{
                ResponseMessage.success(member);
            }

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/loginWithThirdTwo----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     * @param password
     * @param villageId
     */
    @RequestMapping(value="/loginWithThirdThree", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第三步。（共三步）。-1直接提示消息；")
    public void loginWithThirdThree(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
                                  @ApiParam(required=true, name="password", value="密码（明文）")@RequestParam(value = "code", required = true)String password,
                                  @ApiParam(required=true, name="villageId", value="小区ID")@RequestParam(value = "villageId", required = true)Long villageId){

        try{

            SundayMemberDto member= memberService.loginWithThirdThree( thirdType, nickName, image,  unionId,  mobile, code, password,villageId);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/loginWithThirdThree----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版和管理版】退出登录。-1直接提示消息；
     * @param request
     * @param response

     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/logout", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】退出登录。-1直接提示消息；")
    public void logout(HttpServletRequest request,
                                  HttpServletResponse response,
                                   @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                   @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nonce", nonce);
            param.put("sign", sign);
           /* boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.logout(memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/logout----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 【用户版和管理版】修改个人信息。-1直接提示消息；
     * @param request
     * @param response
     * @param nickName
     * @param mobileName
     * @param address
     * @param email
     * @param image
     * @param idCard
     * @param desc
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/update", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改个人信息。-1直接提示消息；会返回新的token，记得更新移动端的token")
    public void update(HttpServletRequest request,
                       HttpServletResponse response,

                       @ApiParam(required=false, name="nickName", value="昵称")@RequestParam(value = "nickName", required = false)String nickName,
                       @ApiParam(required=false, name="mobileName", value="联系人")@RequestParam(value = "mobileName", required = false)String mobileName,
                       @ApiParam(required=false, name="address", value="地址")@RequestParam(value = "address", required = false)String address,
                       @ApiParam(required=false, name="email", value="电子邮箱")@RequestParam(value = "email", required = false)String email,
                       @ApiParam(required=false, name="image", value="头像")@RequestParam(value = "image", required = false)String image,
                       @ApiParam(required=false, name="idCard", value="身份证号码")@RequestParam(value = "idCard", required = false)String idCard,
                       @ApiParam(required=false, name="desc", value="个人简介")@RequestParam(value = "desc", required = false)String desc,
                       @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nickName", nickName);

            param.put("mobileName", mobileName);
            param.put("address", address);
            param.put("email", email);
            param.put("image", image);
            param.put("idCard", idCard);
            param.put("desc", desc);
            param.put("nonce", nonce);
            param.put("sign", sign);
           /* boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            Long memberId = AccountIdHoldUtils.getMemberId();
           SundayMemberDto result =memberService.update( memberId, nickName,mobileName, address, email, image, idCard, desc);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/update----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 【用户版和管理版】修改密码。使用旧密码修改
     * @param request
     * @param response
     * @param oldPassword
     * @param newPassword

     */
    @RequestMapping(value="/updatePassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改密码。使用旧密码修改。-1直接提示消息；")
    public void updatePassword(HttpServletRequest request,
                       HttpServletResponse response,

                       @ApiParam(required = true, name = "oldPassword", value = "旧密码") @RequestParam(value = "oldPassword", required = true) String oldPassword,
                       @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                       @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("oldPassword", oldPassword);
            param.put("newPassword", newPassword);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
           /* if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.updatePassword(memberId,oldPassword, newPassword);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/updatePassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 用户版和管理版】修改密码。使用验证码修改
     * @param request
     * @param response
     * @param mobile
     * @param newPassword
     * @param code
     */
    @RequestMapping(value="/updatePasswordWithCode", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改密码。使用验证码修改。-1直接提示消息；")
    public void updatePasswordWithCode(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                               @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                               @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("mobile", mobile);
            param.put("newPassword", newPassword);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
           /* if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            Long memberId = AccountIdHoldUtils.getMemberId();
           // memberService.updatePassword(memberId,oldPassword, newPassword);
            memberService.updatePasswordWithCode(memberId,mobile,newPassword,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/updatePasswordWithCode----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }
    /**
     * 用户版和管理版】忘记密码
     * @param request
     * @param response
     * @param mobile
     * @param newPassword
     * @param code
     */
    @RequestMapping(value="/forgetPassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】忘记密码。。-1直接提示消息；")
    public void forgetPassword(HttpServletRequest request,
                                       HttpServletResponse response,
                                        @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                       @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                                       @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("mobile", mobile);
            param.put("newPassword", newPassword);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
          /*  if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            memberService.forgetPassword(mobile,newPassword,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/forgetPassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版】设置支付密码。
     * @param request
     * @param response

     * @param payPassword
     * @param code
     */
    @RequestMapping(value="/setPayPassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】设置支付密码。-1直接提示消息；")
    public void setPayPassword(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(required = true, name = "payPassword", value = "支付密码") @RequestParam(value = "payPassword", required = true) String payPassword,
                               @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("payPassword", payPassword);
            param.put("code", code);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
           /* if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.setPayPassword(memberId, payPassword, code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/setPayPassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版】修改支付密码。
     * @param request
     * @param response

     * @param payPassword
     * @param code
     */
    @RequestMapping(value="/updatePayPassWord", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】修改支付密码。。-1直接提示消息；")
    public void updatePayPassWord(HttpServletRequest request,
                               HttpServletResponse response,
                                @ApiParam(required = true, name = "payPassword", value = "支付密码") @RequestParam(value = "payPassword", required = true) String payPassword,
                                @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                                  @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                  @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.updatePayPassWord(memberId, payPassword, code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/updatePayPassWord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 【用户版】校验支付密码
     * @param request
     * @param response
     * @param payPassword
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/checkPayPassWord", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】校验支付密码(提交订单前调用)。-1直接提示消息；")
    public void checkPayPassWord(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required = true, name = "payPassword", value = "支付密码") @RequestParam(value = "payPassword", required = true) String payPassword,

                                  @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                  @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.checkPayPassWord(memberId, payPassword);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/checkPayPassWord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

    /**
     * 获取门店子账号
     * @param request
     * @param response
     *
     */
    @RequestMapping(value="/getDriver", method= RequestMethod.POST)
    @ApiOperation(value="【管理版】获取门店子账号(配送员)。。-1直接提示消息；")
    public void getDriver(
            HttpServletRequest request,
            HttpServletResponse response ,
            @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<Map<String ,Object>> result=memberService.getDeliveryMan(memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/getDriver----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }
    /**
     * 获取门店子账号
     * @param request
     * @param response
     *
     */
    @RequestMapping(value="/findOne", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】获取用户详情，移动端同时更新用户token；")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response ,
            @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            SundayMemberDto result=memberService.findOne(memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }
    /**
     * 个人分享页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/share",method={ RequestMethod.POST,RequestMethod.GET} )
    public  String share(HttpServletRequest request,
                         Model model,
                         @RequestParam(value="id",required=true) long id){
        SundayMember member = memberService.getById(id);

        model.addAttribute("image",member.getImage());
        model.addAttribute("nickName",member.getNickName());
        return "/sunday/member/share";
    }
    /**
     * 会员卡相关
     */
    /**
     * 个人分享页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/card/index",method={ RequestMethod.POST,RequestMethod.GET} )
    public  String card_index(HttpServletRequest request,
                         Model model,
                         @RequestParam(value="memberId",required=true) long memberId){
        //SundayMember member = memberService.getById(id);
        Map<String,Object> result= memberService.member_card_index(memberId);
        model.addAttribute("result",result);
        return "/sunday/member/card/index";
    }

    /**
     * 会员卡绑定-页面
     * @param request
     * @param model
     * @param memberId
     * @return
     */
    @RequestMapping(value="/card/bind/index",method={ RequestMethod.POST,RequestMethod.GET} )
    public  String card_bind_index(HttpServletRequest request,
                              Model model,
                              @RequestParam(value="memberId",required=true) long memberId){
        model.addAttribute("memberId",memberId);
        return "/sunday/member/card/bind";
    }

    /**
     * 会员卡绑定
     * @param request
     * @param response
     * @param memberId
     * @param cardNo
     * @param mobile
     * @param validateCode
     */
    @RequestMapping(value="/card/bind", method= RequestMethod.POST)
    public void card_bind(HttpServletRequest request,
                      HttpServletResponse response,
                     @RequestParam(value = "memberId", required = true)Long memberId,
                     @RequestParam(value = "cardNo", required = true)String cardNo,
                     @RequestParam(value = "mobile", required = true)String mobile,
                     @RequestParam(value = "validateCode", required = true)String validateCode

    ){
        try{
           memberService.member_card_bind(memberId,cardNo,mobile,validateCode);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/card/bind----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 会员卡绑定-页面
     * @param request
     * @param model
     * @param memberId
     * @return
     */
    @RequestMapping(value="/pos/index",method={ RequestMethod.POST,RequestMethod.GET} )
    public  String pos_index(HttpServletRequest request,
                                   Model model,
                                   @RequestParam(value="memberId",required=true) long memberId){
        // Map<String,Object> result = posSaleService.getSale(memberId);
        model.addAttribute("memberId",memberId);

        return "/sunday/member/pos/index";
    }

    /**
     * 获取统计数据
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping(value="/pos/getSale", method= RequestMethod.POST)
    public void loginWithThirdTwo(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestParam(value="memberId",required=true) long memberId){

        try{

           Map<String,Object> result = posSaleService.getSale(request,memberId);
           ResponseMessage.success(result);

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/pos/getSale----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }
}
